using System.Collections.Generic;
using FubuMVC.Core.Runtime;

namespace FubuMVC.Core.Behaviors
{
    public class prepare_the_input<TInput> : IActionBehavior
        where TInput : class, new()
    {
        private readonly IDictionaryConverter _converter;
        private readonly IModelState _modelState;
        private readonly ICurrentAction _currentAction;

        public prepare_the_input(IDictionaryConverter converter, IModelState modelState, ICurrentAction currentAction)
        {
            _converter = converter;
            _modelState = modelState;
            _currentAction = currentAction;
        }

        public IActionBehavior InsideBehavior { get; set; }

        public void Invoke()
        {
            ICollection<ConvertProblem> problems;
            var input = _converter.ConvertFrom<TInput>(_currentAction.CurrentRequest, out problems);

            //TODO: need to deal with the ConvertProblems

            _modelState.ProvideModel(input);

            InsideBehavior.Invoke();
        }
    }
}